package com.sx.gongzhong.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.model.UserInfoBean;
import com.sx.gongzhong.pest.ForgetPwdPest;
import com.sx.gongzhong.pest.VerifyCodePest;
import com.sx.gongzhong.utils.BaseUtil;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.ForgetPwdView;
import com.sx.gongzhong.view.VerifyCodeView;
import com.sx.gongzhong.widget.LoadDialog;
import com.sx.gongzhong.widget.MyRatingBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xiaofei.library.datastorage.DataStorageFactory;
import xiaofei.library.datastorage.IDataStorage;

public class ForgetPwdActivity extends BaseActivity implements VerifyCodeView, ForgetPwdView {

    @BindView(R.id.ll_Code)
    LinearLayout ll_Code;
    @BindView(R.id.et_Phone)
    EditText et_Phone;
    @BindView(R.id.et_Message)
    EditText et_Message;
    @BindView(R.id.tv_GetCode)
    TextView tv_GetCode;
    @BindView(R.id.ll_Set)
    LinearLayout ll_Set;
    @BindView(R.id.et_Password)
    EditText et_Password;
    @BindView(R.id.rb_RatingBar)
    MyRatingBar rb_RatingBar;
    @BindView(R.id.tv_Strong)
    TextView tv_Strong;
    @BindView(R.id.et_AgainPwd)
    EditText et_AgainPwd;

    private long time=60*1000;
    private CountDownTimer timer;
    private String token = "",password = "",confirmPwd = "";


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        setTitleName("找回密码");
        initViews();
    }

    private void initViews() {
        rb_RatingBar.setClickable(false);
        et_Password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()>6&&s.length()<8){
                    tv_Strong.setText("很弱");
                    rb_RatingBar.setStar(1);
                }
                if (s.length()>=8){
                    if ((s.toString()).matches("[0-9]+")||(s.toString()).matches("[a-zA-Z]+")){
                        tv_Strong.setText("一般");
                        rb_RatingBar.setStar(2);
                    }else {
                        tv_Strong.setText("安全");
                        rb_RatingBar.setStar(3);
                    }
                }
            }
        });
    }

    String phone,verifyCode;
    @OnClick({R.id.tv_GetCode,R.id.btn_Submit,R.id.btn_Next})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_GetCode:
                if (!BaseUtil.isEmpty(et_Phone.getText().toString().trim())){
                    new VerifyCodePest(this).getVerifyCode(et_Phone.getText().toString().trim(),2);
                }else {
                    ToastUtils.showToast(ForgetPwdActivity.this,"请先输入手机号码");
                }
                break;
            case R.id.btn_Submit:
                password = et_Password.getText().toString().trim();
                if (BaseUtil.isEmpty(password)){
                    et_Password.setError("请输入密码");
                    return;
                }
                confirmPwd = et_AgainPwd.getText().toString().trim();
                if (BaseUtil.isEmpty(confirmPwd)){
                    et_AgainPwd.setError("请输入确认密码");
                    return;
                }
                LoadDialog.show(ForgetPwdActivity.this,"提交中...");
                new ForgetPwdPest(this).forgetpwd(this,phone,password,confirmPwd,token);
                break;
            case R.id.btn_Next:
                phone = et_Phone.getText().toString().trim();
                if (BaseUtil.isEmpty(phone)){
                    et_Phone.setError("请输入手机号");
                    return;
                }
                if (phone.length() != 11){
                    et_Phone.setError("请输入正确手机号");
                    return;
                }
                verifyCode = et_Message.getText().toString().trim();
                if (BaseUtil.isEmpty(verifyCode)){
                    et_Message.setError("请输入验证码");
                    return;
                }
                new VerifyCodePest(this).checkVerifyCode(phone, verifyCode,2);
                break;
        }
    }

    private void startTime() {
        timer=new CountDownTimer(time,998) {
            @Override
            public void onTick(long millisUntilFinished) {
                time-=1000;
                if(time>0){
                    tv_GetCode.setText(""+time/1000+"s");
                    tv_GetCode.setTextColor(Color.parseColor("#fa4040"));
                }
            }
            @Override
            public void onFinish() {
                tv_GetCode.setText("重新获取");
                tv_GetCode.setTextColor(Color.parseColor("#000000"));
                tv_GetCode.setClickable(true);
            }
        }.start();
    }

    @Override
    protected void onDestroy() {
        if (timer!=null){
            timer.cancel();
            timer.onFinish();
            timer=null;
        }
        super.onDestroy();
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(ForgetPwdActivity.this);
        ToastUtils.showToast(ForgetPwdActivity.this,message);
    }

    @Override
    public void getVerifyCodeSuccess(Boolean result) {
        tv_GetCode.setClickable(false);
        time = 60*1000;
        startTime();
    }

    @Override
    public void checkVerifyCodeSuccess(String result) {
        token = result;
        ll_Set.setVisibility(View.VISIBLE);
        ll_Code.setVisibility(View.GONE);
    }

    @Override
    public void updateforgetpwdSuccess(Boolean result) {
        LoadDialog.dismiss(ForgetPwdActivity.this);
        IDataStorage dataStorage = DataStorageFactory.getInstance(getApplicationContext(),
                DataStorageFactory.TYPE_DATABASE);
        if (dataStorage.contains(UserInfoBean.class,getPackageName())){
            UserInfoBean userBean = dataStorage.load(UserInfoBean.class,getPackageName());
            userBean.password = password;
            dataStorage.storeOrUpdate(userBean,getPackageName());
        }
        finish();
    }
}
